﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;

namespace EmailHelper.tool
{
    /// <summary>
    /// 设置配置文件的键值
    /// </summary>
    public class ConfigHelper
    {
        public static bool setConfigValue(String key,String value)
        {
            try
            {
                Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                if (configuration.AppSettings.Settings[key] != null)
                {
                    configuration.AppSettings.Settings[key].Value = value;
                }
                else
                {
                    configuration.AppSettings.Settings.Add(key, value);
                }
                configuration.Save(ConfigurationSaveMode.Modified);
                ConfigurationManager.RefreshSection("appSettings");
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public static String GetConfig(String key)
        {
            Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            if (configuration.AppSettings.Settings[key] != null)
            {
                return configuration.AppSettings.Settings[key].Value;
            }
            else
            {
                return String.Empty;
            }
        }
    }
}
